﻿
var WShellHelper = {

    //注册表的网页打印路径
    key_root: "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\",

    setPrintOrientation: function (isHorizontal) {
        var isSuccess = true;
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            //打印页面的Menubar必须可见，此操作类似按键盘上的Alt+F+U也就是 调出页面设置对话框
            wsShell.sendKeys('%fu');

            //此处必须要延时一定时间后执行。延时时间不能太长，测试结果不能超过30毫秒，本机测试10毫秒合适。
            setTimeout(function () {

                if (isHorizontal) {
                    //此操作类似按键盘上的Alt+A也就是 设置横向打印
                    wsShell.sendKeys('%a');
                }
                else {
                    //此操作类似按键盘上的Alt+O也就是 设置横向打印
                    wsShell.sendKeys('%o');
                }

                //此操作类似按键盘上的回车 页面设置对话框的默认焦点在 确定上 所以直接确定
                wsShell.sendKeys('{ENTER}');

            }, 2);

        }
        catch (e) {
            alert(e.message);
            isSuccess = false;
        }
        return isSuccess;
    },

    setPrintHeader: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "header", value);
        } catch (e) {
            alert(e.message);
        }
    },

    setPrintFooter: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "footer", value);
        } catch (e) {
            alert(e.message);
        }
    },

    setPrintMarginLeft: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "margin_left", value);
        } catch (e) {
            alert(e.message);
        }
    },

    setPrintMarginRight: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "margin_right", value);
        } catch (e) {
            alert(e.message);
        }
    },

    setPrintMarginTop: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "margin_top", value);
        } catch (e) {
            alert(e.message);
        }
    },

    setPrintMarginBottom: function (value) {
        try {
            var wsShell = new ActiveXObject("WScript.Shell");
            wsShell.RegWrite(this.key_root + "margin_bottom", value);
        } catch (e) {
            alert(e.message);
        }
    }
};

